home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / MATH / BIN32 / BIN32.ZIP / BIN32.PAS < prev    next >
Pascal/Delphi Source File  |  1996-03-25  |  16KB  |  459 lines

  1.  
  2. { Demoprogram that uses unit Binary32.
  3.   Binary32.dcu is compiled with Delphi« 2.0 (32-bit).
  4.   If you do not have this compiler, contact me and I'll send you a 16-bit version
  5.   via e-mail (mail attachment).
  6.  
  7.   Integer (base 10), Binary (base 2), Octal (base 8), Hexadecimal (base 16).
  8.  
  9.   Implemented functions:
  10.   ----------------------
  11.   Integer -> Binary and vice versa;
  12.   Integer -> Octal and vice versa;
  13.   Integer -> Hexadecimal and vice versa;
  14.   Binary -> Octal and vice versa;
  15.   Binary -> Hexadecimal and vice versa;
  16.   Octal -> Hexadecimal and vice versa;
  17.   Binary +, -, *, /;
  18.   Binary logic: OR, XOR, AND, NOT;
  19.   Binary 1 bit/byte left/right shift.
  20.  
  21.   Copyright ⌐1996, John Merrit
  22.   Contact: john.merrit@net4all.be
  23.   Release History: Version 1.32, released March 25 (finally!) 1996.
  24.  
  25.   This code is provided as is for your use.  It may
  26.   be modified and redistributed.
  27.  
  28.   This code is not to be sold and must be distributed
  29.   for free.
  30.  
  31.   Unit Binary32.dcu may be used freely (you don't need to register your copy) and may
  32.   not be sold or distributed for commercial purposes.
  33.   ( Just use it the way you use a unit included with Delphi«. )
  34.  
  35.   Possible modifications:
  36.   ------------------------
  37.   Addition of:
  38.   Binary logic for decimal numbers (very easy when you have the code for Binary32!);
  39.   Current largest hexadecimal value = FFFFFFF (= 268435455 decimal = 1777777777 octal = 11...11 binary);
  40.   User definable wordsize;
  41.   ...
  42.  100011001111001101100011000000001101100000000001101110111001111111001111110011111001000000000011110000
  43.  
  44.  Function definitions in unit Binary32:
  45.  ----------------------------------------
  46.  General notes:
  47.  1. Integer must be a positive ( > 0) integer ( NO fractional part)!
  48.  2. #FFFFFFFh = #1777777777o = #111...111b = #268435455d is (currently) the greatest
  49.     number supported.
  50.  3. User does not need to define a wordsize.
  51.     Get the source code for Binary32.dcu (see BinLog32.txt) and implement this
  52.     yourself (you already got 24 (if you count them, it's actually 48) functions from me for free!).
  53.  
  54.  type NumberBase = (Hex, Oct, Bin, Dec); // Note that Dec is not used in Binary32.
  55.                                          // Only implemented for future updates.
  56.  
  57.  function IntToBin(LI: longint): string;    // Integer to Binary
  58.  function BinToInt(LI: string): longint;    // Binary to Integer
  59.  function IntToOct(LI: longint): string;    // Integer to Octal
  60.  function MyIntToHex(LI: longint): string;  // Integer to Hexadecimal (variant on Borland's IntToHex)
  61.  function OctToInt(LI: string): longint;    // Octal to Integer
  62.  function HexToInt(LI: string): longint;    // Hexadecimal to Integer
  63.  function BinToOct(LI: string): string;     // Binary to Octal
  64.  function OctToBin(LI: string): string;     // Octal to Binary
  65.  function BinToHex(LI: string): string;     // Binary to Hexadecimal
  66.  function HexToBin(LI: string): string;     // Hexadecimal to Binary
  67.  function OctToHex(LI: string): string;     // Octal to Hexadecimal
  68.  function HexToOct(LI: string): string;     // Hexadecimal to Octal
  69.  // The following functions can only be applied on numbers of base Bin, Oct and Hex.
  70.  function BinAdd(B1,B2: string; Base: NumberBase): string;  // Binary addition (B1 + B2)
  71.  function BinSubtract(B1,B2: string; Base: NumberBase): string; // Binary subtraction (B1 - B2)
  72.  function BinMultiply(B1,B2: string; Base: NumberBase): string; // Binary Multiplication (B1 * B2)
  73.  function BinDivide(B1,B2: string; Base: NumberBase): string; // Binary division (B1 div B2)
  74.  function BinOR(B1,B2: string; Base: NumberBase): string; // Binary B1 OR B2
  75.  function BinAND(B1,B2: string; Base: NumberBase): string; // Binary B1 AND B2
  76.  function BinXOR(B1,B2: string; Base: NumberBase): string; // Binary B1 XOR B2
  77.  function BinNOT(B1: string; Base: NumberBase): string; // Binary NOT B1
  78.  function BinShiftLeft(B1: string; Base: NumberBase): string; // 1 Bit left shift on a binary integer
  79.  function BinShiftRight(B1: string; Base: NumberBase): string; // 1 Bit right shift on a binary integer
  80.  function BinShiftLeftByte(B1: string; Base: NumberBase): string; // 1 Byte left shift on a binary integer
  81.  function BinShiftRightByte(B1: string; Base: NumberBase): string; // 1 Byte right shift on a binary integer
  82.  
  83. }
  84.  
  85. unit Bin32;
  86.  
  87. interface
  88.  
  89. uses
  90.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  91.   Menus, StdCtrls, Binary32;
  92.  
  93. type
  94.   TForm1 = class(TForm)
  95.     Memo1: TMemo;
  96.     MainMenu1: TMainMenu;
  97.     Edit1: TMenuItem;
  98.     Start1: TMenuItem;
  99.     N1: TMenuItem;
  100.     Exit1: TMenuItem;
  101.     Font1: TMenuItem;
  102.     FontDialog1: TFontDialog;
  103.     Clear1: TMenuItem;
  104.     procedure Exit1Click(Sender: TObject);
  105.     procedure Start1Click(Sender: TObject);
  106.     procedure Font1Click(Sender: TObject);
  107.     procedure Clear1Click(Sender: TObject);
  108.   private
  109.     { Private declarations }
  110.   public
  111.     { Public declarations }
  112.   end;
  113.  
  114. var
  115.   Form1: TForm1;
  116.  
  117. implementation
  118.  
  119. {$R *.DFM}
  120.  
  121. procedure TForm1.Exit1Click(Sender: TObject);
  122. begin
  123.   Close;
  124. end;
  125.  
  126. { Main program.
  127.   Note that no errortrapping is done!
  128.   Not all the implemented functions are showcased below.
  129.   Look at the definitions above for proper usage.
  130.   The program has been written so that no error will occur.
  131.   If you change anything and an error does occur, it's your responsibility, not mine!}
  132. procedure TForm1.Start1Click(Sender: TObject);
  133. var h,i,j: longint;
  134.     k,l,m: integer;
  135.     s1,s2: string;
  136.     Base: NumberBase;
  137. begin
  138.      randomize;
  139.      for m:= 1 to 5 do
  140.      begin                  { Convert random generated integers to bin, oct and hex... }
  141.      i:= random(50000) + 1;
  142.      Memo1.Lines.Add(IntToStr(i) + ' to binary: ' + IntToBin(i));
  143.      Memo1.Lines.Add(IntToStr(i) + ' to octal: ' + IntToOct(i));
  144.      Memo1.Lines.Add(IntToStr(i) + ' to hexadecimal: ' + MyIntToHex(i));
  145.      end;
  146.      for m:= 1 to 5 do
  147.      begin
  148.      h:= random(10000) +1; { don't make it 0 }
  149.      j:= random(1000) +1; { don't make it 0 }
  150.      k:= random(3) +1; { don't make it 0 }
  151.      l:= random(4)+1; { don't make it 0 }
  152.      case k of
  153.      1:begin     { Do some arithmetic on random generated integers. }
  154.        Base:= Bin;        { Let the program decide wich base it will use... }
  155.        s1:= IntToBin(h);
  156.        s2:= IntToBin(j);
  157.        Memo1.Lines.Add('Base = Bin');
  158.        Memo1.Lines.Add('h= ' + s1 + ' j = ' + s2);
  159.        case l of
  160.        1: Memo1.Lines.Add('h + j in Bin = ' + BinAdd(s1,s2,Base));
  161.        2: Memo1.Lines.Add('h - j in Bin = ' + BinSubtract(s1,s2,Base));
  162.        3: Memo1.Lines.Add('h * j in Bin = ' + BinMultiply(s1,s2,Base));
  163.        4: Memo1.Lines.Add('h / j in Bin = ' + BinDivide(s1,s2,Base));
  164.        end;
  165.        end;
  166.      2:begin
  167.        Base:= Oct;
  168.        s1:= IntToOct(h);
  169.        s2:= IntToOct(j);
  170.        Memo1.Lines.Add('Base = Oct');
  171.        Memo1.Lines.Add('h= ' + s1 + ' j = ' + s2);
  172.        case l of
  173.        1: Memo1.Lines.Add('h + j in Oct = ' + BinAdd(s1,s2,Base));
  174.        2: Memo1.Lines.Add('h - j in Oct = ' + BinSubtract(s1,s2,Base));
  175.        3: Memo1.Lines.Add('h * j in Oct = ' + BinMultiply(s1,s2,Base));
  176.        4: Memo1.Lines.Add('h / j in Oct = ' + BinDivide(s1,s2,Base));
  177.        end;
  178.        end;
  179.      3:begin
  180.        Base:= Hex;
  181.        s1:= MyIntToHex(h);
  182.        s2:= MyIntToHex(j);
  183.        Memo1.Lines.Add('Base = Hex');
  184.        Memo1.Lines.Add('h= ' + s1 + ' j = ' + s2);
  185.        case l of
  186.        1: Memo1.Lines.Add('h + j in Hex = ' + BinAdd(s1,s2,Base));
  187.        2: Memo1.Lines.Add('h - j in Hex = ' + BinSubtract(s1,s2,Base));
  188.        3: Memo1.Lines.Add('h * j in Hex = ' + BinMultiply(s1,s2,Base));
  189.        4: Memo1.Lines.Add('h / j in Hex = ' + BinDivide(s1,s2,Base));
  190.        end;
  191.        end;
  192.      end;
  193.      end;
  194.      for m:= 1 to 5 do       { The following code is added in a hurry. }
  195.      begin                   { It isn't as structured as above.        }
  196.      i:= random(50000) + 1; { It should still be very readable, though... }
  197.      j:= random(50000) + 1; { The program will calculate the OR, XOR and AND on random numbers... }
  198.      s1:= IntToOct(i);      { ...in each base.    }
  199.      s2:= IntToOct(j);
  200.      Memo1.Lines.Add('Base is octal: ');
  201.      Memo1.Lines.Add('i= ' + s1);
  202.      Memo1.Lines.Add('h= ' + s2);
  203.      s1:= BinOR(s1,s2,Oct);
  204.      Memo1.Lines.Add(' i OR h: ' + s1);
  205.      end;
  206.      for m:= 1 to 5 do
  207.      begin
  208.      i:= random(50000) + 1;
  209.      j:= random(50000) + 1;
  210.      s1:= IntToOct(i);
  211.      s2:= IntToOct(j);
  212.      Memo1.Lines.Add('Base is octal: ');
  213.      Memo1.Lines.Add('i= ' + s1);
  214.      Memo1.Lines.Add('h= ' + s2);
  215.      s1:= BinXOR(s1,s2,Oct);
  216.      Memo1.Lines.Add('i XOR h: ' + s1);
  217.      end;
  218.      for m:= 1 to 5 do
  219.      begin
  220.      i:= random(50000) + 1;
  221.      j:= random(50000) + 1;
  222.      s1:= IntToOct(i);
  223.      s2:= IntToOct(j);
  224.      Memo1.Lines.Add('Base is octal: ');
  225.      Memo1.Lines.Add('i= ' + s1);
  226.      Memo1.Lines.Add('h= ' + s2);
  227.      s1:= BinAND(s1,s2,Oct);
  228.      Memo1.Lines.Add('i AND h: ' + s1);
  229.      end;
  230.      for m:= 1 to 5 do
  231.      begin
  232.      i:= random(50000) + 1;
  233.      j:= random(50000) + 1;
  234.      s1:= MyIntToHex(i);
  235.      s2:= MyIntToHex(j);
  236.      Memo1.Lines.Add('Base is hexadecimal: ');
  237.      Memo1.Lines.Add('i= ' + s1);
  238.      Memo1.Lines.Add('h= ' + s2);
  239.      s1:= BinOR(s1,s2,Hex);
  240.      Memo1.Lines.Add('i OR h: ' + s1);
  241.      end;
  242.      for m:= 1 to 5 do
  243.      begin
  244.      i:= random(50000) + 1;
  245.      j:= random(50000) + 1;
  246.      s1:= MyIntToHex(i);
  247.      s2:= MyIntToHex(j);
  248.      Memo1.Lines.Add('Base is hexadecimal: ');
  249.      Memo1.Lines.Add('i= ' + s1);
  250.      Memo1.Lines.Add('h= ' + s2);
  251.      s1:= BinXOR(s1,s2,Hex);
  252.      Memo1.Lines.Add('i XOR h: ' + s1);
  253.      end;
  254.      for m:= 1 to 5 do
  255.      begin
  256.      i:= random(50000) + 1;
  257.      j:= random(50000) + 1;
  258.      s1:= MyIntToHex(i);
  259.      s2:= MyIntToHex(j);
  260.      Memo1.Lines.Add('Base is hexadecimal: ');
  261.      Memo1.Lines.Add('i= ' + s1);
  262.      Memo1.Lines.Add('h= ' + s2);
  263.      s1:= BinAND(s1,s2,Hex);
  264.      Memo1.Lines.Add('i AND h: ' + s1);
  265.      end;
  266.      for m:= 1 to 5 do
  267.      begin
  268.      i:= random(50000) + 1;
  269.      j:= random(50000) + 1;
  270.      s1:= IntToBin(i);
  271.      s2:= IntToBin(j);
  272.      Memo1.Lines.Add('Base is binary: ');
  273.      Memo1.Lines.Add('i= ' + s1);
  274.      Memo1.Lines.Add('h= ' + s2);
  275.      s1:= BinOR(s1,s2,Bin);
  276.      Memo1.Lines.Add('i OR h: ' + s1);
  277.      end;
  278.      for m:= 1 to 5 do
  279.      begin
  280.      i:= random(50000) + 1;
  281.      j:= random(50000) + 1;
  282.      s1:= IntToBin(i);
  283.      s2:= IntToBin(j);
  284.      Memo1.Lines.Add('Base is binary: ');
  285.      Memo1.Lines.Add('i= ' + s1);
  286.      Memo1.Lines.Add('h= ' + s2);
  287.      s1:= BinXOR(s1,s2,Bin);
  288.      Memo1.Lines.Add('i XOR h: ' + s1);
  289.      end;
  290.      for m:= 1 to 5 do
  291.      begin
  292.      i:= random(50000) + 1;
  293.      j:= random(50000) + 1;
  294.      s1:= IntToBin(i);
  295.      s2:= IntToBin(j);
  296.      Memo1.Lines.Add('Base is binary: ');
  297.      Memo1.Lines.Add('i= ' + s1);
  298.      Memo1.Lines.Add('h= ' + s2);
  299.      s1:= BinAND(s1,s2,Bin);
  300.      Memo1.Lines.Add('i AND h: ' + s1);
  301.      end;
  302.      { NOT was last added, so... }
  303.      for m:= 1 to 5 do
  304.      begin
  305.      i:= random(50000) + 1;
  306.      s1:= IntToBin(i);
  307.      Memo1.Lines.Add('Base is binary: ');
  308.      Memo1.Lines.Add('i= ' + s1);
  309.      s1:= BinNOT(s1,Bin);
  310.      Memo1.Lines.Add('NOT i: ' + s1);
  311.      end;
  312.      for m:= 1 to 5 do
  313.      begin
  314.      i:= random(50000) + 1;
  315.      s1:= IntToOct(i);
  316.      Memo1.Lines.Add('Base is octal: ');
  317.      Memo1.Lines.Add('i= ' + s1);
  318.      s1:= BinNOT(s1,Oct);
  319.      Memo1.Lines.Add('NOT i: ' + s1);
  320.      end;
  321.      for m:= 1 to 5 do
  322.      begin
  323.      i:= random(50000) + 1;
  324.      s1:= MyIntToHex(i);
  325.      Memo1.Lines.Add('Base is hexadecimal: ');
  326.      Memo1.Lines.Add('i= ' + s1);
  327.      s1:= BinNOT(s1,Hex);
  328.      Memo1.Lines.Add('NOT i: ' + s1);
  329.      end;
  330.      { Added 24-th, late at night... }
  331.      for m:= 1 to 5 do
  332.      begin
  333.      i:= random(50000) + 1;
  334.      s1:= MyIntToHex(i);
  335.      Memo1.Lines.Add('Base is hexadecimal: ');
  336.      Memo1.Lines.Add('i= ' + s1);
  337.      s1:= BinShiftLeft(s1,Hex);
  338.      Memo1.Lines.Add('Binary 1 bit shift left: ' + s1);
  339.      end;
  340.      for m:= 1 to 5 do
  341.      begin
  342.      i:= random(50000) + 1;
  343.      s1:= MyIntToHex(i);
  344.      Memo1.Lines.Add('Base is hexadecimal: ');
  345.      Memo1.Lines.Add('i= ' + s1);
  346.      s1:= BinShiftRight(s1,Hex);
  347.      Memo1.Lines.Add('Binary 1 bit shift right: ' + s1);
  348.      end;
  349.      for m:= 1 to 5 do
  350.      begin
  351.      i:= random(50000) + 1;
  352.      s1:= MyIntToHex(i);
  353.      Memo1.Lines.Add('Base is hexadecimal: ');
  354.      Memo1.Lines.Add('i= ' + s1);
  355.      s1:= BinShiftLeftByte(s1,Hex);
  356.      Memo1.Lines.Add('Binary 1 byte shift left: ' + s1);
  357.      end;
  358.      for m:= 1 to 5 do
  359.      begin
  360.      i:= random(50000) + 1;
  361.      s1:= MyIntToHex(i);
  362.      Memo1.Lines.Add('Base is hexadecimal: ');
  363.      Memo1.Lines.Add('i= ' + s1);
  364.      s1:= BinShiftRightByte(s1,Hex);
  365.      Memo1.Lines.Add('Binary 1 byte right shift: ' + s1);
  366.      end;
  367.      for m:= 1 to 5 do
  368.      begin
  369.      i:= random(50000) + 1;
  370.      s1:= IntToOct(i);
  371.      Memo1.Lines.Add('Base is octal: ');
  372.      Memo1.Lines.Add('i= ' + s1);
  373.      s1:= BinShiftLeft(s1,Oct);
  374.      Memo1.Lines.Add('Binary 1 bit shift left: ' + s1);
  375.      end;
  376.      for m:= 1 to 5 do
  377.      begin
  378.      i:= random(50000) + 1;
  379.      s1:= IntToOct(i);
  380.      Memo1.Lines.Add('Base is octal: ');
  381.      Memo1.Lines.Add('i= ' + s1);
  382.      s1:= BinShiftRight(s1,Oct);
  383.      Memo1.Lines.Add('Binary 1 bit shift right: ' + s1);
  384.      end;
  385.      for m:= 1 to 5 do
  386.      begin
  387.      i:= random(50000) + 1;
  388.      s1:= IntToOct(i);
  389.      Memo1.Lines.Add('Base is octal: ');
  390.      Memo1.Lines.Add('i= ' + s1);
  391.      s1:= BinShiftLeftByte(s1,Oct);
  392.      Memo1.Lines.Add('Binary 1 byte shift left: ' + s1);
  393.      end;
  394.      for m:= 1 to 5 do
  395.      begin
  396.      i:= random(50000) + 1;
  397.      s1:= IntToOct(i);
  398.      Memo1.Lines.Add('Base is octal: ');
  399.      Memo1.Lines.Add('i= ' + s1);
  400.      s1:= BinShiftRightByte(s1,Oct);
  401.      Memo1.Lines.Add('Binary 1 byte right shift: ' + s1);
  402.      end;
  403.      for m:= 1 to 5 do
  404.      begin
  405.      i:= random(5000) + 1;
  406.      s1:= IntToBin(i);
  407.      Memo1.Lines.Add('Base is binary: ');
  408.      Memo1.Lines.Add('i= ' + s1);
  409.      s1:= BinShiftLeft(s1,Bin);
  410.      Memo1.Lines.Add('Binary 1 bit shift left: ' + s1);
  411.      end;
  412.      for m:= 1 to 5 do
  413.      begin
  414.      i:= random(5000) + 1;
  415.      s1:= IntToBin(i);
  416.      Memo1.Lines.Add('Base is binary: ');
  417.      Memo1.Lines.Add('i= ' + s1);
  418.      s1:= BinShiftRight(s1,Bin);
  419.      Memo1.Lines.Add('Binary 1 bit shift right: ' + s1);
  420.      end;
  421.      for m:= 1 to 5 do
  422.      begin
  423.      i:= random(5000) + 1;
  424.      s1:= IntToBin(i);
  425.      Memo1.Lines.Add('Base is binary: ');
  426.      Memo1.Lines.Add('i= ' + s1);
  427.      s1:= BinShiftLeftByte(s1,Bin);
  428.      Memo1.Lines.Add('Binary 1 byte shift left: ' + s1);
  429.      end;
  430.      for m:= 1 to 5 do
  431.      begin
  432.      i:= random(5000) + 1;
  433.      s1:= IntToBin(i);
  434.      Memo1.Lines.Add('Base is binary: ');
  435.      Memo1.Lines.Add('i= ' + s1);
  436.      s1:= BinShiftRightByte(s1,Bin);
  437.      Memo1.Lines.Add('Binary 1 byte right shift: ' + s1);
  438.      end;
  439.      Memo1.Lines.Add('Terminat hora diem, terminat auctor opus.');
  440.  
  441. end;
  442.  
  443. { To ensure a readability on everyones screen... }
  444. procedure TForm1.Font1Click(Sender: TObject);
  445. begin
  446.   if FontDialog1.Execute then
  447.   Memo1.Font:= FontDialog1.Font;
  448. end;
  449.  
  450. procedure TForm1.Clear1Click(Sender: TObject);
  451. begin
  452.    if MessageDlg('Clear this memo?', mtInformation,
  453.       [mbOk, mbCancel], 0) = id_OK then
  454.       Memo1.Lines.Clear;
  455.  
  456. end;
  457.  
  458. end.
  459.